<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/19
 * Time: 10:44
 */
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\request;

/**
 *
 * 个人资料
 */
class Personal extends Base{

    //线上支付页面
    public function index(){
            $ste=Db::name('users')->where('id='.Session::get('uid'))->find();
            //是否实名
            if($ste['RNA']!=1){
                $this->success('请先去实名认证','certification/index');
            }
            $list=Db::name('user_certification')->where('uid='.Session::get('uid').' and status=1')->find();
            if(!$list){
                $list['names']='';
                $list['IDnumbers']='';
            }else{
                //姓名
                $list['names']=substr($list['name'],0,3).'*'.substr($list['name'],-3);
                //身份证、
                $list['IDnumbers']=substr($list['IDnumber'],0,10).'****'.substr($list['IDnumber'],-4);
            }
            //电话号码
            $list['tap']=substr($ste['phonenumber'],0,3).'****'.substr($ste['phonenumber'],-4);

            $this->assign('list',$list);
            $this->assign('ste',$ste);
        return $this->fetch('Personal/index');

    }

    //获取短信验证码
    public function passwordCode(){
        $param = $this->request->post();
        $param=myarr($param);
        $mobile = $param['mobile'];
        if(!$mobile) return json_encode(['msg'=>'手机号不能为空'],JSON_UNESCAPED_UNICODE);
        if($param['s']=='swt'){
            $tpCode = 'SMS_135026381';//支付密码添加
        }elseif($param['s']=='ret'){
            $tpCode = 'SMS_135026381';//消息变更
        }elseif($param['s']=='modify'){
            $tpCode = 'SMS_134815251';
        }
        $tplParam = rand(1000,9999);
        $user = Db::name('users')->where('id='.Session::get('uid'))->find();
//        return json_encode(['msg'=>$param['oldmobile']]);
        if($mobile==$user['phonenumber']){
            //一分钟只能发生一次
            $usercode = Db::name('user_code')->where('mobile',createCode($mobile))->order('id desc')->find();
            // dump($usercode);die;
            if($usercode['reg_time']+60 >= time()) return json_encode(['msg'=>'一分钟只能发送一次'],JSON_UNESCAPED_UNICODE);
            $message = sendMsg(Session::get('phonenumber'),$tpCode,$tplParam);
            // $message = ['Message'=>'OK'];
            if($message['Message']=='OK'){
                $data = [
                    'mobile' => $mobile,
                    'code'   => $tplParam,
                    'reg_time'=> time(),
                ];
                $code = Db::name('user_code')->insert($data);
                if($code){
                    return json_encode(['msg'=>'success'],JSON_UNESCAPED_UNICODE);
                }else{
                    return json_encode(['msg'=>'验证码发送错误'],JSON_UNESCAPED_UNICODE);
                }
            }else{
                return json_encode(['msg'=>'验证码发送错误'],JSON_UNESCAPED_UNICODE);
            }
        }elseif($tpCode == 'SMS_134815251'){
            $mobile = createCode($param['mobile']);
//            return json_encode(['msg'=>$mobile],JSON_UNESCAPED_UNICODE);
//            die;
            $m = Db::name('users')->where(['phonenumber'=>$mobile])->find();
            if($m) return json_encode(['msg'=>'手机号已经被注册'],JSON_UNESCAPED_UNICODE);
            $usercode = Db::name('user_code')->where('mobile',$mobile)->order('id desc')->find();
            // dump($usercode);die;
            if($usercode['reg_time']+60 >= time())  return json_encode(['msg'=>'一分钟只能发送一次'],JSON_UNESCAPED_UNICODE);
            // return json_encode(['msg'=>$param['mobile']]);
            $message = sendMsg($param['mobile'],$tpCode,$tplParam);
            // $message = ['Message'=>'OK'];
            if($message['Message']=='OK'){
                $data = [
                    'mobile' => $mobile,
                    'code'   => $tplParam,
                    'reg_time'=> time(),
                ];
                $code = Db::name('user_code')->insert($data);
                if($code){
                    return json_encode(['msg'=>'success'],JSON_UNESCAPED_UNICODE);
                }else{
                    return json_encode(['msg'=>'验证码发送错误'],JSON_UNESCAPED_UNICODE);
                }
            }else{
                return json_encode(['msg'=>'验证码发送错误'],JSON_UNESCAPED_UNICODE);
            }
        }else{
            return json_encode(['msg'=>'输入的手机号有误'],JSON_UNESCAPED_UNICODE);
        }

    }
    //手机验证
    public function Verificationmobile(){
        $param = $this->request->post();
        $param=myarr($param);
        $mobiles = createCode($param['mobiles']);
        if($param['mobiles'] == null ){
            return json_encode(['msg'=>'请输入手机号'],JSON_UNESCAPED_UNICODE);
        }
        $user = Db::name('users')->where(['phonenumber'=>createCode($mobiles)])->find();
        if($user){
            return json_encode(['msg'=>'用户已经被注册'],JSON_UNESCAPED_UNICODE);
        }else{
            return json_encode(['msg'=>'success']);
        }
    }
    //登录电话号码修改
    public function editphoneNumber(){
        $user = Db::name('users')->where('id='.Session::get('uid'))->find();
        //获取短信验证码
        $param = $this->request->post();
        $param=myarr($param);
        $jmmobile = createCode($param['mobile']);
        $oldmobile = $user['phonenumber'];
        //此手机号是否注册
        $arr=Db::name('users')->where('phonenumber',$jmmobile)->find();
        if($arr!=''){
            return json_encode(['msg'=>'此手机号已存在'],JSON_UNESCAPED_UNICODE);
        }

        if($user['phonenumber'] != $oldmobile){
            return json_encode(['msg'=>'错误'],JSON_UNESCAPED_UNICODE);
        }

        if($user['phonenumber'] == $jmmobile){
            return json_encode(['msg'=>'和原账户相同不能提交'],JSON_UNESCAPED_UNICODE);
        }

        if($oldmobile!=$jmmobile && $jmmobile && $param['oldcode'] && $param['newcode']){
            //旧手机验证码验证
            $code = Db::name('user_code');
            $oldcode = $code->where(['mobile'=>$oldmobile])->order('id desc')->find();
            $newcode = $code->where(['mobile'=>$jmmobile])->order('id desc')->find();

            if($oldcode['code'] != $param['oldcode']){
                return json_encode(['msg'=>'旧号码验证错误'],JSON_UNESCAPED_UNICODE);
            }
            if($newcode['code'] != $param['newcode']){
                return json_encode(['msg'=>'新号码验证错误'],JSON_UNESCAPED_UNICODE);
            }

            $update = Db::name('users')->where(['id'=>$user['id']])->update(['phonenumber'=>$jmmobile]);
            if($update){
                Session::set('phonenumber',$param['mobile']);
                //操作记录
                $cao=array();
                $cao['logintoip']= $_SERVER['REMOTE_ADDR'];
                $cao['uid']=Session::get('uid');
                $cao['logintime'] = date('Y-m-d H:i:s');
                $cao['operation']="修改手机号;原号码:".createCode($param['oldmobile']);
                Db::table('qfll_login_record')->insert($cao);
                Db::name('user_code')->where('mobile',$jmmobile)->update(['state'=>1]);
                Db::name('user_code')->where('mobile',$oldmobile)->update(['state'=>1]);

                return json_encode(['msg'=>'success']);
            }else{
                return json_encode(['msg'=>'修改失败']);
            }
        }else{
            return json_encode(['msg'=>'请检查资料在提交'],JSON_UNESCAPED_UNICODE);
        }

    }

}